package com.maowei.init;

import com.maowei.QuickCanalClientListener;
import com.maowei.event.EventHandlerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * 监听器初始化操作
 *
 * 1.将实现了AbstractEventHandler接口, 并带有@TableEvent注解的Bean注入到事件处理工厂中
 * 2.启动canal客户端监听 while(true) 循环监听binlog日志的产生
 *
 * @author maowei
 * @date 2021/8/25
 */
@Component
public class ListenerInitialization implements ApplicationRunner {

    private final EventHandlerFactory eventHandlerFactory;
    private final QuickCanalClientListener canalClientListener;

    public ListenerInitialization(EventHandlerFactory eventHandlerFactory, QuickCanalClientListener canalClientListener) {
        this.eventHandlerFactory = eventHandlerFactory;
        this.canalClientListener = canalClientListener;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 自动注册 环境里的事件处理器
        eventHandlerFactory.autoRegister();
        // 启动canal客户端监听
        try {
            canalClientListener.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
